<?php

/**
 * modePaiement actions.
 *
 * @package    ProjetBar
 * @subpackage modePaiement
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class modePaiementActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->mode_paiements = Doctrine_Core::getTable('modePaiement')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->mode_paiement = Doctrine_Core::getTable('modePaiement')->find(array($request->getParameter('id_type_paiement'),
                                        $request->getParameter('id_commande_client')));
    $this->forward404Unless($this->mode_paiement);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new modePaiementForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new modePaiementForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($mode_paiement = Doctrine_Core::getTable('modePaiement')->find(array($request->getParameter('id_type_paiement'),
                  $request->getParameter('id_commande_client'))), sprintf('Object mode_paiement does not exist (%s).', $request->getParameter('id_type_paiement'),
                  $request->getParameter('id_commande_client')));
    $this->form = new modePaiementForm($mode_paiement);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($mode_paiement = Doctrine_Core::getTable('modePaiement')->find(array($request->getParameter('id_type_paiement'),
                  $request->getParameter('id_commande_client'))), sprintf('Object mode_paiement does not exist (%s).', $request->getParameter('id_type_paiement'),
                  $request->getParameter('id_commande_client')));
    $this->form = new modePaiementForm($mode_paiement);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($mode_paiement = Doctrine_Core::getTable('modePaiement')->find(array($request->getParameter('id_type_paiement'),
                  $request->getParameter('id_commande_client'))), sprintf('Object mode_paiement does not exist (%s).', $request->getParameter('id_type_paiement'),
                  $request->getParameter('id_commande_client')));
    $mode_paiement->delete();

    $this->redirect('modePaiement/index');
  }
  
  public function executePaiement(sfWebRequest $request)
  {
	$this->idCommandeClient = $request->getParameter('id_commande_client');
	$this->idTypePaiement = $request->getParameter('id_type_paiement');
	$this->libelleTypePaiement = Doctrine_Core::getTable('typePaiement')->find($this->idTypePaiement);
	$this->idTableClient = $request->getParameter('id_table_client');
	$requeteCartes = Doctrine_Query::Create()
		->select('pc.id_produit, pv.prix_unitaire_produit_en_vente, pc.quantite_produit, pv.libelle_produit, c.libelle_categorie')
		->from('produitEnVente pv')
		->innerJoin('pv.Produit p')
		->innerJoin('pv.ProduitEnCommandeALaCarte pc')
		->innerJoin('pv.Categorie c')
		->where('pc.id_commande_client = '.$this->idCommandeClient.'')
		->orderBy('c.libelle_categorie');
	$this->produitCartes = $requeteCartes->fetchArray();
	
	
	$this->produitMenus = Doctrine_Query::Create()
		->select('cm.id_commande_menu, m.id_menu as id_menu, m.libelle_menu, m.prix_unitaire_menu,pcm.id_produit, pv.libelle_produit as libelle_produit')
		->from('commandeMenu cm')		
		->innerJoin('cm.Menu m')
		->innerJoin('cm.ProduitEnCommandeViaMenu pcm')
		->innerJoin('cm.CommandeClient cc')
		->innerJoin('pcm.ProduitEnVente pv')		
		->where('cc.id_commande_client = :id_commande_client', array(':id_commande_client' => $this->idCommandeClient))
		->fetchArray();
	//print_r($this->produitMenus[0]);
  }
  
  public function executeValiderPaiement(sfWebRequest $request)
  {
	$this->idCommandeClient = $request->getParameter('id_commande_client');
	$this->idTypePaiement = $request->getParameter('id_type_paiement');
	$this->idTableClient = $request->getParameter('id_table_client');
	$modePaiement = new modePaiement();
	$modePaiement->id_type_paiement = $this->idTypePaiement;
	$modePaiement->id_commande_client = $this->idCommandeClient;
	$modePaiement->save();
	
	Doctrine_Query::Create()
		->update('commandeClient c')
		->set('c.id_etat_commande',2)
		->where('c.id_commande_client = '.$this->idCommandeClient.'')
		->execute();
	
	$this->redirect('commandeClient/commandesParTable?id_table_client='.$this->idTableClient.'');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $mode_paiement = $form->save();

      $this->redirect('modePaiement/edit?id_type_paiement='.$mode_paiement->getIdTypePaiement().'&id_commande_client='.$mode_paiement->getIdCommandeClient());
    }
  }
}
